<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{

    public function __construct($application)
    {
        parent::__construct($application);        
    }

	public function run()
    {
    	// enabled FirePHP
    	$logger = new Zend_Log();
		$writer = new Zend_Log_Writer_Firebug();
		$logger->addWriter($writer);
		Zend_Registry::set('logger',$logger);
    	// set the default timezone
    	date_default_timezone_set('US/Eastern');
        // make the config available to everyone
        Zend_Registry::set('config', new Zend_Config($this->getOptions()));
        /* 
        use this to output logs to FirePHP
		Zend_Registry::get('logger')->log('This is a log message!', Zend_Log::INFO);
        */
        
        parent::run();
    }
	
    protected function _initAppAutoload() {
		$loader = new Zend_Application_Module_Autoloader ( array (
							'namespace' => '', 
							'basePath'  => dirname(__FILE__),
					 ));
		return $loader;
	}
	
	protected function _initSession()
    {
        //we will always use session, so this is good place to create this and save it to the registry
    	$session = new Zend_Session_Namespace('socialgiving', true);
        Zend_Registry::set('session', $session);
        
        return $session;        
    }
	
    protected function _initViewHelpers() 
    {
		$layout = Zend_Layout::startMvc();
		// Tell the view where it finds Zend_Dojo ViewHelper
		$layout->getView()
			->addHelperPath('App/View/Helper/', 'App_View_Helper')		
			->addHelperPath('Zend/Dojo/View/Helper/', 'Zend_Dojo_View_Helper');		

    }

	protected function _initView() 
	{ 
		// Initialize view
		$view = new Zend_View(array('encoding'=>'UTF-8'));
        $view->doctype('XHTML1_STRICT');

        // setup initial head place holders
        $view->headMeta()->setName('Content-Type', 'text/html; charset=utf-8');
        $view->headMeta()->setName('description', 'xxx');
        $view->headMeta()->setName('keywords', 'xxx');
		
        // Add it to the ViewRenderer
        $view->headTitle('Social Giving', 'PREPEND');
        $view->headTitle()->setSeparator(' - ');
        $view->headTitle($view->title);
        
        // Add Helper Path
        $view->addHelperPath('App/View/Helper/', 'App_View_Helper')		
			 ->addHelperPath('Zend/Dojo/View/Helper/', 'Zend_Dojo_View_Helper');
        
        $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
        $viewRenderer->setView($view);
        
        return $view;	
	}
    
	public function _initDoctrine()
	{
        $this->getApplication()->getAutoloader()
            ->pushAutoloader(array('Doctrine', 'autoload'));
        spl_autoload_register(array('Doctrine', 'modelsAutoload'));

        $doctrineConfig = $this->getOption('doctrine');
        $manager = Doctrine_Manager::getInstance();
        $manager->setAttribute(Doctrine_Core::ATTR_AUTO_ACCESSOR_OVERRIDE, true);
        $manager->setAttribute(Doctrine_Core::ATTR_AUTOLOAD_TABLE_CLASSES, true);
        $manager->setAttribute(
          Doctrine_Core::ATTR_MODEL_LOADING,
          $doctrineConfig['model_autoloading']
        );
        
        Doctrine_Core::loadModels($doctrineConfig['models_path']);

        $conn = Doctrine_Manager::connection($doctrineConfig['dsn'],'doctrine');
        $conn->setAttribute(Doctrine_Core::ATTR_USE_NATIVE_ENUM, true);
        Zend_Registry::set('doctrine_manager', $manager->getCurrentConnection());
        
        return $conn;
    }
}